package com.dingding.activity.facade;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.stereotype.Component;

import com.dingding.activity.core.activity.service.IActivityService;
import com.dingding.activity.core.activity.service.ICouponService;
import com.dingding.facde.activity.enums.ActivityEnum.CouponStatus;
import com.dingding.facde.activity.service.IActivityFacade;

/**
 * 
 * @Description: 活动服务接口
 * @author CHEHUANBO
 * @date 2016年12月13日
 * @version V2.6.2
 * 
 */
@Component("activityFacade")
public class ActivityFacadeImpl implements IActivityFacade {

	private static final Logger log = Logger.getLogger(ActivityFacadeImpl.class);

	@Resource(name = "_couponService")
	private ICouponService couponService;

	@Resource(name = "_activityService")
	private IActivityService activityService;

	/**
	 * 
	 * @Description: 领取优惠券
	 * @return Map<String, Object> 
	 * 
	 * resStatus true 业务访问成功 false: 业务访问失败 
	 * errorMsg
	 *         业务访问失败提示信息 
	 * grantCouponStatus： true:领取失败 false:领取失败
	 * 
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Override
	public Map<String, Object> joinActivity(Map<String, Object> params) {

		Map<String, Object> resMap = new HashMap<String, Object>();

		if (params != null) {

			String userId = (String) params.get("userId");

			if (StringUtils.isBlank(userId)) {
				resMap.put("resStatus", false);
				resMap.put("errorMsg", "参数不合法，存在空值!" + resMap.toString());
				log.error("调用领取优惠券服务被拒绝！原因：参数不合法，userId is null");
				return resMap;
			}

			try {

				activityService.joinActivityService(userId);

				resMap.put("resStatus", true);
				resMap.put("grantCouponStatus", true);

			} catch (Exception e) {
				log.error("领取优惠券出现异常！", e);
				resMap.put("resStatus", false);
				resMap.put("errorMsg", "参数不合法，存在空值!" + resMap.toString());
			}

		} else {
			resMap.put("resStatus", false);
			resMap.put("errorMsg", "参数不合法!");
			log.error("调用领取优惠券服务被拒绝！原因：参数不合法，params is null");
		}

		return resMap;

	}

	/**
	 * 
	 * @Description: 验证用户是否领取过优惠券
	 * @param userId
	 *            用户ID
	 * @param activityId
	 *            活动ID
	 * @return Map<String, Object>
	 * resStatus 
	 *         true 业务访问成功 false: 业务访问失败 
	 * errorMsg
	 *         业务访问失败提示信息 
	 * grantCouponStatus： true:领取失败 false:领取失败
	 * 
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Override
	public Map<String, Object> isReceiveCoupon(Map<String, Object> params) {

		Map<String, Object> resMap = new HashMap<String, Object>();

		if (params != null) {

			String userId = (String) params.get("userId");
			String activityId = (String) params.get("activityId");

			if (StringUtils.isBlank(userId) || StringUtils.isBlank(activityId)) {
				resMap.put("resStatus", false);
				resMap.put("errorMsg", "参数不合法，存在空值!" + resMap.toString());
				log.error("调用验证用户是否领取过优惠券被拒绝！原因：参数不合法，存在空值!");
				return resMap;
			}

			boolean isGrant = true;

			try {

				isGrant = couponService.isGrantCoupon(userId, activityId);

			} catch (Exception e) {
				log.error("验证用户是否领取过优惠券出现异常" + params.toString(), e);
				resMap.put("resStatus", false);
				resMap.put("resMasg", "服务器异常!");
			}

			resMap.put("resStatus", true);
			resMap.put("isReceiveCoupon", isGrant);

			return resMap;

		}

		resMap.put("resStatus", false);
		resMap.put("errorMsg", "参数不合法!");

		return resMap;

	}

	/**
	 * 
	 * @Description: 验证优惠券是否发放完成
	 * @author CHEHUANBO
	 * @version V2.6.2
	 * 
	 */
	@Override
	public Map<String, Object> isGrantCouponComplete(Map<String, Object> params) {

		Map<String, Object> resMap = new HashMap<String, Object>();

		if (params != null) {

			String activityId = (String) params.get("activityId");

			if (StringUtils.isBlank(activityId)) {
				resMap.put("resStatus", false);
				resMap.put("resMasg", "参数不合法！" + params.toString());
				log.error("调用验证优惠券是否发放完成被拒绝！原因：参数不合法，存在空值!");
			}

			String cityNo = (String) params.get("cityNo");

			CouponStatus couponStatus = CouponStatus.couponInvalid;

			try {
				couponStatus = couponService.isReceiveCouponByActivityId(activityId, cityNo);
			} catch (Exception e) {
				log.error("验证优惠券是否发放完成出现异常" + params.toString(), e);
				resMap.put("resStatus", false);
				resMap.put("resMasg", "服务器异常!");
			}

			resMap.put("resStatus", true);
			resMap.put("resMasg", "");
			resMap.put("isGrantComplete", couponStatus);

		} else {
			resMap.put("resStatus", false);
			resMap.put("resMasg", "参数不合法！");
		}

		return resMap;

	}
	
	
	

}
